<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Quanlytintuc extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 Thu
	 */
	function __construct()
    {
        parent::__construct();	
		$this->data['tieude']="Quản Lý Tin Tức";
		$this->loadadmin();
	}
	public function index()
	{	
		$this->load->model('Tintuc_model');	
		$this->data=$this->Tintuc_model->getresult_index();
		$this->load->view('tintuc/tintuc',$this->data);
		$this->load->view('layout/footer');
	}
	public function chitiet($matt)
	{
		$this->load->model('Tintuc_model');
		$this->data['tintuc']=$this->Tintuc_model->gettintuc($matt);
		$this->load->view('tintuc/chitiettintuc',$this->data);
		$this->load->view('layout/footer');
	}

	public function them()
	{		
		$this->load->library('form_validation');
		$this->load->model('Tintuc_model');
		
		$this->data['thongtin']=$this->input->post();
		if(!empty($this->data['thongtin']))
		{
			if($this->form_validation->run('themtintuc') ){
				$filHinhDaiDien = "filHinhDaiDien";
				
				if(!($_FILES[$filHinhDaiDien]['error']==4))
				{	
					$this->data['img']=	$this->upload_process($filHinhDaiDien);	
					if(!empty($this->data['img']['image_full_url']))
					{
						$this->data['thongtin']=$this->input->post();	
						$this->data['thongtin']['hinh']=$this->data['img']['image_full_url'];
						$now=getdate();
						$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 
						$this->data['thongtin']['ngaydang']=$currentDate;
						$this->data['thongtin']['ma_tk']='1';
						//$this->data['thongtin']['ma_tk']=session_name('taikhoan');
						$this->Tintuc_model->insert($this->data['thongtin']);
						$this->data['thongbao']="Thêm thành công tin tức ".$this->data['thongtin']['tieude']."!";	
					}	
				}else 
				{
					$this->data['img']['error_upload']="chưa chọn hình";
				}	
			}	
		}
		$this->load->view('tintuc/themtintuc',$this->data);
		$this->load->view('layout/footer');
	}
	public function sua($matt)
	{		
		$this->load->library('form_validation');
		$this->load->model('Tintuc_model');
		
		$this->data['thongtin']=$this->input->post();
		if(!empty($this->data['thongtin']))
		{
			if($this->form_validation->run('themtintuc') ){
				$filHinhDaiDien = "filHinhDaiDien";
				
				if(!($_FILES[$filHinhDaiDien]['error']==4))
				{	
					$this->data['img']=	$this->upload_process($filHinhDaiDien);	
					if(!empty($this->data['img']['image_full_url']))
					{
						$this->data['thongtin']=$this->input->post();	
						$this->data['thongtin']['hinh']=$this->data['img']['image_full_url'];
						
						$this->data['thongtin']['ma_tk']='1';
						//$this->data['thongtin']['ma_tk']=session_name('taikhoan');
						$this->Tintuc_model->xoahinhtintuc($matt);
						$this->Tintuc_model->update($this->data['thongtin'],$matt);
						$this->data['thongbao']="Sửa thành công tin tức ".$this->data['thongtin']['tieude']."!";	
					}	
				}else 
				{
						$this->data['thongtin']=$this->input->post();	
						$this->data['thongtin']['ma_tk']='1';
						//$this->data['thongtin']['ma_tk']=session_name('taikhoan');
						$this->Tintuc_model->update($this->data['thongtin'],$matt);
						$this->data['thongbao']="Sửa thành công tin tức ".$this->data['thongtin']['tieude']."!";	
				}	
			}	
		}
		$this->data['tintuc']=$this->Tintuc_model->gettintuc($matt);
		$this->load->view('tintuc/suatintuc',$this->data);
		$this->load->view('layout/footer');
	}
	public function xoa($matt)
	{
		$this->load->model('Tintuc_model');
		$this->Tintuc_model->delete($matt);
		$dt= $this->input->get();
		if(empty($dt['page']))
		{
			$dt['page']=1;
		}
		if(!empty($dt['key']))	
		redirect("/Quanlytintuc/index/?key={$dt['key']}&page={$dt['page']}", 'refresh');
		else 
    	redirect("/Quanlytintuc/index/?page={$dt['page']}", 'refresh');
	}
	public function upload_process($upload_field) {
		$result = array();
		
		// Upload file
		$this->load->config('upload.php');
		$upload_config = $this->config->item('upload');
		
		$this->load->library('upload', $upload_config);
		$this->upload->initialize($upload_config);
		
		if (!$this->upload->do_upload($upload_field))
		{
			// Upload bị lỗi
			$result['error_upload'] = $this->upload->display_errors();
			$result['image_full_url'] = '';
		}
		else
		{
			// Upload file successfull
			$result['upload_data'] = $this->upload->data();
			
			if (isset($result['upload_data']['is_image']) && $result['upload_data']['is_image'] == true) {
				// File vừa tải lên là file ảnh hợp lệ
				$result['image_full_url'] =  $upload_config['upload_path'] . $result['upload_data']['file_name'];
			}
			else {
				// File vừa tải lên không phải là file ảnh hợp lệ
				$result['image_full_url'] = '';
			}
		}
		
		return $result;
	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */